package com.lq.schoolshop.controller.before;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lq.schoolshop.bean.Result;
import com.lq.schoolshop.bean.StatusCode;
import com.lq.schoolshop.dao.GoodsMapper;
import com.lq.schoolshop.pojo.BigScreen;
import com.lq.schoolshop.pojo.Goods;
import com.lq.schoolshop.pojo.Store;
import com.lq.schoolshop.service.BigScreenService;
import com.lq.schoolshop.service.GoodsService;
import com.lq.schoolshop.service.StoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Date2021/1/7 10:46
 * @Version V1.0
 * 主页：商品查询、轮播、商品全部、所有店铺
 **/

@Api(value = "商城前台功能--主页模块",tags = "商城前台功能--主页模块")
@CrossOrigin(origins = "*",maxAge = 3600)
@RequestMapping("main")
@Controller
public class ForMainIndexController {

    @Autowired
    private BigScreenService bigScreenService;

    @Autowired
    private StoreService storeService;

    @Autowired
    private GoodsService goodsService;

    //1.动态加载首页轮播
    @ApiOperation(value = "加载首页轮播",notes = "用来加载首页的轮播")
    @ResponseBody
    @RequestMapping(value = "/ShowBigScreenAll",method = RequestMethod.GET)
    public Result ShowBigScreenAll(){
        Result result = null;
        List<BigScreen> bigScreens = bigScreenService.selByAll();
        if(bigScreens!=null){
            result = new Result(true, StatusCode.OK,"加载轮播成功",bigScreens);
        }else{
            result = new Result(true, StatusCode.NotSource,"加载轮播失败",bigScreens);
        }
        return result;
    }

    //2.加载全部店铺信息
    @ApiOperation(value = "加载店铺信息",notes = "用来加载店铺的名称和图标,显示在首页")
    @ResponseBody
    @RequestMapping(value = "/ShowStoreAll",method = RequestMethod.GET)
    public Result ShowStoreAll(){
        Result result = null;
        List<Store> stores = storeService.selByAll();
        if(stores!=null){
            result = new Result(true, StatusCode.OK,"加载店铺信息成功",stores);
        }else{
            result = new Result(true, StatusCode.RESOURCEINFOISNULL,"加载店铺信息失败",stores);
        }
        return result;
    }


    //3.加载全部商品
    @ApiOperation(value = "加载商品信息--非分页",notes = "加载全部商品的名称/图标/价格/详情信息 显示在首页")
    @ResponseBody
    @RequestMapping(value = "/ShowGoodsByRecommend",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    public String ShowGoodsByRecommend(){
        //Result result = null;
        Map<String,Object>map = new HashMap<>();
        List<Goods> goodsMappers = goodsService.selByall();
        if(goodsMappers!=null){
            map.put("code",200);
            map.put("msg","加载店铺信息成功");
            map.put("data",goodsMappers);
            //result = new Result(true, StatusCode.OK,"加载店铺信息成功",goodsMappers);
        }else{
            map.put("code",201);
            map.put("msg","加载店铺信息失败");
          //  result = new Result(true, StatusCode.OK,"加载店铺信息失败",goodsMappers);
        }
        String result = JSONObject.toJSONString(map);
        return result;
    }

    //3.1加载全部商品--流加载分页显示
    @ApiOperation(value = "加载商品信息--分页",notes = "加载并分页显示全部商品的名称/图标/价格/详情信息 显示在首页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页面",dataType = "String",required = true,paramType = "query"),
            @ApiImplicitParam(name = "pageSize",value = "显示数量",dataType = "String",required = true,paramType = "query"),
    })
    @ResponseBody
    @RequestMapping(value = "/ShowGoodsByPage",method = RequestMethod.GET,produces = "application/json;charset=utf-8")
    public String ShowGoodsByPage(@RequestParam("pageNum")String pageNum,@RequestParam("pageSize") String pageSize){
        Map<String,Object> map = new HashMap<>();
        System.out.println("***流加载分页信息***");
        int pageNumint = Integer.parseInt(pageNum);
        int pageSizeint = Integer.parseInt(pageSize);
        PageHelper.startPage(pageNumint, pageSizeint);
        List<Goods> goods = goodsService.selByall();
        PageInfo pageInfo = new PageInfo(goods);
        map.put("code",200);
        map.put("msg","加载分页数据成功");
        map.put("data",pageInfo);
        String result = JSONObject.toJSONString(map);
        //Result result = new Result(true,StatusCode.OK,"加载分页数据成功",pageInfo);
        return result;
    }


    //4.根据商品名进行模糊搜索
    @ApiOperation(value = "商品搜索",notes = "在主页搜索栏上进行商品名的模糊搜索")
    @ApiImplicitParam(name = "name",value = "商品名称",dataType = "String",required = true,paramType = "query")
    @ResponseBody
    @RequestMapping(value = "/ShowGoodsByName",method = RequestMethod.GET)
    public Result ShowGoodsByName(@RequestParam("name")String name,@RequestParam("pageNum")String pageNum,@RequestParam("pageSize") String pageSize){
        int pageNumint = Integer.parseInt(pageNum);
        int pageSizeint = Integer.parseInt(pageSize);
        PageHelper.startPage(pageNumint, pageSizeint);
        List<Goods> goodsMappersByname = goodsService.selByName(name);
        PageInfo pageInfo = new PageInfo(goodsMappersByname);
        Result result = null;
        if(goodsMappersByname!=null && goodsMappersByname.size()>0){
            result = new Result(true, StatusCode.OK,"加载商品信息成功",pageInfo);
        }else{
            result = new Result(true, StatusCode.OK,"加载商品信息失败或该商品不存在",goodsMappersByname);
        }
        return result;
    }


}
